package com.hetang.maijiayun.adaptertools.b2cinterface.dangdang.client;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.hetang.maijiayun.adaptertools.b2cinterface.dangdang.domain.ErrorResponse;
import com.hetang.maijiayun.adaptertools.b2cinterface.dangdang.request.DdGetOrderRequest;
import com.hetang.maijiayun.adaptertools.b2cinterface.dangdang.request.DdRequest;
import com.hetang.maijiayun.adaptertools.b2cinterface.dangdang.response.DdGetOrderResponse;
import com.hetang.maijiayun.adaptertools.b2cinterface.dangdang.response.DdResponse;
import com.hetang.maijiayun.adaptertools.utils.AdapterException;
import com.hetang.maijiayun.adaptertools.utils.AdapterExceptionErrorCode;
import com.hetang.maijiayun.adaptertools.utils.FileItem;
import com.hetang.maijiayun.adaptertools.utils.Md5;
import com.hetang.maijiayun.adaptertools.utils.WebUtils;
import com.hetang.maijiayun.adaptertools.utils.XmlUtil;

public class DdClient {
	private static String format = "xml";
	private static String v = "1.0";
	private static String sign_method = "md5";
	private static int connectTimeout = 1000;
	private static int readTimeout = 100000;
	private static String charset = "GBK";
	private static DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private String url = null;
	private String appKey = null;
	private String secret = null;
	private String session = null;
	private String method;
	private Map<String, FileItem> fileParams;
	
	
	public DdClient(String url, String appKey, String secret, String session) {
		this.url = url;
		this.appKey = appKey;
		this.secret = secret;
		this.session = session;
	}
	
	public <T extends DdResponse, E extends DdRequest> T execute(E req, Class<T> clazz) throws AdapterException {
		if(req.getFileParams()!=null && req.getFileParams().size()>0){
	    	method  = "POST";
	    	fileParams = req.getFileParams();
	    }
		Map<String, String> paras = new HashMap<String, String>();
		paras.put("app_key", appKey);
		paras.put("format", format);
		paras.put("method", req.getName());
		paras.put("session", session);
		paras.put("sign_method", sign_method);
		String timestamp = dFormat.format(new Date());
		paras.put("timestamp", timestamp);		
		paras.put("v", v);
		paras.put("sign", Md5.getMd5(secret + "app_key" + appKey + "format" + format 
				+ "method" + req.getName() + "session" + session + "sign_method" + sign_method
				+ "timestamp" + timestamp + "v" + v + secret).toUpperCase());
		paras.putAll(req);
		
		try {
			String res;
			if (fileParams != null && fileParams.size() > 0) {
				res = WebUtils.doPost(url, paras, fileParams, charset, connectTimeout, readTimeout);
			} else if ("POST".equalsIgnoreCase(method)) {
				res = WebUtils.doPost(url, paras, charset, connectTimeout, readTimeout);
			}else{
				res = WebUtils.doGet(url, paras, charset);	
			}
//			System.out.println(res);	//TODO 上线时注释掉
			try {
				return XmlUtil.getBean(res, clazz);
			} catch(AdapterException ae) {
				ErrorResponse response = null;
				try {
					response = XmlUtil.getBean(res, ErrorResponse.class);
				} catch(AdapterException e) {
				}
				
				if(response != null) {
					throw new AdapterException(AdapterExceptionErrorCode.B2C_ERROR_CODE, "Method : " + req.getName() + "ErrorCode : " + response.getErrorCode() + " ErrorMsg : " + response.getErrorMessage());
				} else 
					throw new AdapterException(AdapterExceptionErrorCode.B2C_ERROR_CODE, ae.getErrorCode() + "|" + ae.getMessage());					
			}
		} catch(IOException ie) {
			throw new AdapterException(AdapterExceptionErrorCode.B2C_ERROR_CODE, ie.getMessage());
		}
	}
	
	public DdGetOrderResponse execute(DdGetOrderRequest req) throws AdapterException {
		return execute(req, DdGetOrderResponse.class);
	}
}